home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-04-27 | 2.2 KB | 109 lines | [TEXT/MPS ] |
- /*
- VirtualWorld.cp
-
- Implementation of Virtual world maintenance class.
-
- This class maintains an A5 global space, as well as supporting
- initialization of virtual function tables at run-time.
-
- by Patrick Beard.
-
- based on TN 256.
-
- ©1990 by Patrick C. Beard. All rights reserved.
- */
-
- #ifndef __VIRTUALWORLD__
- #include "VirtualWorld.h"
- #endif
- #ifndef __EXCEPTIONS__
- #include "Exceptions.h"
- #endif
-
- #ifndef __QUICKDRAW__
- #include <QuickDraw.h>
- #endif
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- extern "C" {
- void A5Init(void*); // function that sets up (initializes) the A5 world.
- long A5Size(void); // function that returns the size of our A5 world.
- void init_vtbls(void); // function that initializes virtual function tables.
-
- // utility routine to switch A5's. (lifted from OSUtils.h)
- #pragma parameter __A0 SwapA5(__A0)
- void* SwapA5(void* newA5) = 0xC14D;
- }
-
-
- // constructor: set up the current code's required A5 world.
-
- VirtualWorld::VirtualWorld(Boolean worldFloats)
- {
- itsFloating = worldFloats;
- itsSize = A5Size(); // store our size for speed.
- itsWorld = new char[itsSize]; // allocate storage for our globals.
-
- // initialize our globals.
- itsA5 = itsWorld + itsSize - 32; // see TN 256.
- A5Init(itsA5);
-
- // call InitGraf to initialize quickdraw globals & get a valid port.
- GrafPtr port;
- GetPort(&port);
-
- void* oldWorld = Enter(); // go inside our world.
- InitGraf(&qd.thePort);
- SetPort(port);
-
- // if this code doesn't float, only call this function once.
- if (!itsFloating)
- init_vtbls();
-
- Leave(oldWorld); // leave the virtual world.
- }
-
- VirtualWorld::~VirtualWorld()
- {
- delete itsWorld;
- }
-
- void* VirtualWorld::Enter()
- {
- GrafPtr currPort;
- GetPort(&currPort);
-
- void* oldA5 = SwapA5(itsA5);
-
- SetPort(currPort);
-
- if (itsFloating) {
- // call virtual table initialization functions everytime our world is
- // entered. this way, if we've been relocated, the tables will be correct.
- init_vtbls();
- }
-
- return oldA5;
- }
-
- void VirtualWorld::Leave(void* oldA5)
- {
- SwapA5(oldA5);
- }
-
- void* operator new(size_t n)
- {
- void* p = NewPtr(n);
- if (!p)
- throw(memFullErr);
- return p;
- }
-
- void operator delete(void* p)
- {
- if (p)
- DisposPtr((Ptr)p);
- }
-